﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HOPETechnik.XMLParser;

namespace TestSocket
{
    abstract class CBaseRequest : CXMLMemStream
    {        
        public abstract void Reset();
        public string strXMLFragment
        {
            set { m_strXMLFragment = value; }
        }
    }

    

    //request flyweights
    class CRequestFactory
    {
        Dictionary<string, CBaseRequest> m_requests;
        string[] RootElementNames;        

        public CRequestFactory()
        {
            RootElementNames = new string[] {"<TextMessage>", "<FileSendReq>", "<FileSendResp>", "<BinaryContentSendCompleted>"};
            m_requests = new Dictionary<string, CBaseRequest>();            
        }

        public CBaseRequest getRequest(string strXMLFragment)
        {
            CBaseRequest req = null;  
            
            foreach(string str in RootElementNames)
            {
                if (strXMLFragment.IndexOf(str)>-1)
                {
                    if (!m_requests.ContainsKey(str))
                    {
                        switch (str)
                        {
                            case "<TextMessage>":
                                req = new CTextMessage();                                
                                break;
                            case "<FileSendReq>":
                                req = new CFileSendRequest();
                                break;
                            case "<FileSendResp>":
                                req=new CFileSendResp();
                                break;
                           
                        }
                        if (req!=null)
                            m_requests[str] = req;
                    }
                    req = m_requests[str];
                    req.Reset();
                    req.strXMLFragment = strXMLFragment;
                    break;
                }                
            }
            return req;
        }
    }
}
